package com.syezon.wifikey.redbag;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.syezon.wifikey.ad.BoxAdViewModel;
import com.syezon.wifikey.base.BaseFragment;
import com.syezon.wifikey.R;
import com.syezon.wifikey.redbag.adapter.BoxAdapter;
import com.syezon.wifikey.redbag.info.BoxInfo;
import com.syezon.wifikey.utils.StatusBarUtil;

import java.util.List;

import butterknife.BindView;

public class BoxFragment extends BaseFragment {

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_box)
    RecyclerView mRvBox;
    private BoxAdapter mBoxAdapter;
    private BoxAdViewModel mBoxAdViewModel;

    public BoxFragment() {
    }

    public static BoxFragment newInstance() {
        BoxFragment fragment = new BoxFragment();
        return fragment;
    }

    @Override
    protected View getLoadingTargetView() {
        return mRefreshLayout;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(getContext(), mRlTop);
        mBoxAdapter = new BoxAdapter(getContext());
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 4);
        GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int spanSize = 1;
                List<BoxInfo> data = mBoxAdapter.getData();
                BoxInfo boxInfo = data.get(position);
                int type = boxInfo.getType();
                if (type == BoxInfo.TYPE_TITLE || type == BoxInfo.TYPE_DIVIDER) {
                    spanSize = manager.getSpanCount();
                }
                if (type == BoxInfo.TYPE_AD_TOOLS) {
                    spanSize = manager.getSpanCount() / 4;
                }
                if (type == BoxInfo.TYPE_AD_PIC_TEXT) {
                    spanSize = manager.getSpanCount() / 2;
                }
                return spanSize;
            }
        };
        manager.setSpanSizeLookup(spanSizeLookup);
        mRvBox.setLayoutManager(manager);
        BoxItemDecoration boxItemDecoration = new BoxItemDecoration(spanSizeLookup, DensityUtil.dp2px(15));
        mRvBox.addItemDecoration(boxItemDecoration);
        mRvBox.setAdapter(mBoxAdapter);
        getAd();
    }

    private void getAd() {
        mBoxAdViewModel = ViewModelProviders.of(this).get(BoxAdViewModel.class);
        mBoxAdViewModel.getAdLiveData().observe(this, new Observer<List<BoxInfo>>() {
            @Override
            public void onChanged(@Nullable List<BoxInfo> boxInfos) {
                mBoxAdapter.setData(boxInfos);
            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_box;
    }
}
